require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIMythicBuffSelect = {};
HelperFunc.SetUIEnv(UIMythicBuffSelect)
local this = UIMythicBuffSelect;

this.gameObject = nil;
this.transform = nil;
this.clicks = {};
this.closeBtn = nil;
this.hasInit = false;

this.objGrid = nil;
this.objBuffItem = nil;
this.btnConfirmBtn = nil;

this.buffIds = nil;
this.tableBuffItem = {};
this.selectBuffId = 0;

this.clickItem = nil;


function UIMythicBuffSelect.Init()
    selectBuffId = 0;
    local name, packageName = getUIName(PanelNames.UIMythicBuffSelect);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIMythicBuffSelect.onCreate(obj)
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        objGrid = obj:GetChild("Grid");
        closeBtn = obj:GetChild("bg");
        btnConfirmBtn = obj:GetChild("ConfirmBtn");
        this.addEvent();
    end
    this.refresh();
end

function UIMythicBuffSelect.addEvent()
    closeBtn.onClick:Add(this.onClose);
    btnConfirmBtn.onClick:Add(this.onClickOnConfirm);
    objGrid.onClickItem:Add(this.onClickBuffIem);
end

function UIMythicBuffSelect.removeEvent()
    closeBtn.onClick:Remove(this.onClose);

    btnConfirmBtn.onClick:Remove(this.onClickOnConfirm);
    objGrid.onClickItem:Remove(this.onClickBuffIem);
end

function UIMythicBuffSelect.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_MythicSelectBuff, this.onClose);
end

function UIMythicBuffSelect.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_MythicSelectBuff, this.onClose);
end

function UIMythicBuffSelect.OnDestroy()
    this.removeEvent();
end

function UIMythicBuffSelect.OnEnable()
    this.LocalListenEvent();
end

function UIMythicBuffSelect.OnDisable()
    this.LocalCloseEvent();
    if this.clickItem ~= nil then
        this.clickItem.selected = false
    end
end

function UIMythicBuffSelect.refresh()
    --刷新函数
    this.buffIds = logicMgr.MythicDungeonsProManager.getWaitBuffIds();

    objGrid.itemRenderer = this.itemRender;
    objGrid.numItems = #this.buffIds;
end

function UIMythicBuffSelect.itemRender(idx, obj)
    local des = obj:GetChild("Des");
    local icon = obj:GetChild("icon"):GetChild("icon");
    local name = obj:GetChild("nameBuff")
    local id = this.buffIds[idx+1]
    ObjUtil.SetBuffIcon(id,icon,name,des)
end

function UIMythicBuffSelect.onClickOnConfirm()
    if selectBuffId > 0 then
        --mythicDungeonsHandler.sendSelectBuff(logicMgr.MythicDungeonsProManager.getLayer(), selectBuffId);
        mythicDungeonsProHandler.sendSelectBuff(logicMgr.MythicDungeonsProManager.getLayer(), selectBuffId);
    else
        UISysTips.AddMsg("请选择祝福")
    end
end

function UIMythicBuffSelect.onClickBuffIem(context)
    local go = context.data;
    local buffId = objGrid:GetChildIndex(go) + 1;
    this.clickItem = go;
    selectBuffId = buffIds[buffId];
end

function UIMythicBuffSelect.onClose()
    closeUI(PanelNames.UIMythicBuffSelect);
end

return UIMythicBuffSelect;
